from flask import request
from flask_restful import Resource

from database import Database


class UpdateStack(Resource):
    def put(self, stackId):

        db = Database()
        query = "SELECT * FROM stack WHERE stackId = %s"
        db.execute_query(query, (stackId,))
        stack = db.cursor.fetchall()

        if not stack:
            return {"message": "Stack does not exist"}, 404

        # Get the updated data from the request
        data = request.json

        # Update the stack attributes with the new values
        categoryId = data.get('categoryId')
        imgUrl = data.get('imgUrl')
        title = data.get('title')
        description = data.get('description')
        author = data.get('author')
        status = data.get('status')
        quantity = data.get('quantity')

        # Update the stack in the database
        query = """UPDATE stack SET categoryId = %s, imgUrl = %s, title = %s, description = %s,
                   author = %s, status = %s, quantity = %s WHERE stackId = %s"""
        db.execute_query(query, (categoryId, imgUrl, title, description, author, status, quantity, stackId))
        db.connection.commit()

        return {"message": "Stack updated successfully"}




